package com.bluegalaxy.sitescore.service.impl;

import com.bluegalaxy.sitescore.entity.GroundWater;
import com.bluegalaxy.sitescore.entity.TbGroundWater;
import com.bluegalaxy.sitescore.entity.TbScore;
import com.bluegalaxy.sitescore.mapper.TbGroundWaterMapper;
import com.bluegalaxy.sitescore.service.IGroundWaterService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GroundWaterServiceImpl implements IGroundWaterService{


    @Autowired
    private TbGroundWaterMapper tbGroundWaterMapper;

    @Override
    public boolean judegeGroundWater(GroundWater groundWater) {
        if(StringUtils.isBlank(groundWater.getAquifer_name())){
            return false;
        }
        if(groundWater.getObserved_release()!=0&&groundWater.getObserved_release()!=550){
            return false;
        }
        if(groundWater.getContainment()!=0&&groundWater.getContainment()!=3&&groundWater.getContainment()!=5&&
        groundWater.getContainment()!=7&&groundWater.getContainment()!=9&&groundWater.getContainment()!=10){
            return false;
        }
        if(groundWater.getNet_precip()!=0&&groundWater.getNet_precip()!=1&&groundWater.getNet_precip()!=3&&
                groundWater.getNet_precip()!=6&&groundWater.getNet_precip()!=10){
            return false;
        }
        if(groundWater.getDepth_to_auuifer()!=1&&groundWater.getDepth_to_auuifer()!=3&&groundWater.getDepth_to_auuifer()!=5){
            return false;
        }
        if(groundWater.getTravel_time()!=1&&groundWater.getTravel_time()!=5&&groundWater.getTravel_time()!=15&&
                groundWater.getTravel_time()!=25&&groundWater.getTravel_time()!=35){
            return false;
        }
        if(groundWater.getNear_well()!=0&&groundWater.getNear_well()!=2&&groundWater.getNear_well()!=3&&
                groundWater.getNear_well()!=5&&groundWater.getNear_well()!=9&&groundWater.getNear_well()!=18&&
                groundWater.getNear_well()!=20&&groundWater.getNear_well()!=45&&groundWater.getNear_well()!=50){
            return false;
        }
        if(groundWater.getResource()!=0&&groundWater.getResource()!=5){
            return false;
        }
        if(groundWater.getWell_pro_area()!=0&&groundWater.getWell_pro_area()!=5&&groundWater.getWell_pro_area()!=20){
            return false;
        }

        return true;
    }

    @Override
    public TbGroundWater caculateGroundWater(GroundWater groundWater,int sourceId) {
        BigDecimal uncappedScore = groundWater.getLikelihood_of_release().multiply(groundWater.getTarget()).multiply(
                BigDecimal.valueOf(groundWater.getWaste_characteristics())).divide(
                BigDecimal.valueOf(82500),2, RoundingMode.DOWN);
        BigDecimal score ;
        if(uncappedScore.floatValue()>100){
            score = BigDecimal.valueOf(100);
        }else {
            score = uncappedScore;
        }

        TbGroundWater tbGroundWater = new TbGroundWater();
        if(groundWater.getId()!=0){
            tbGroundWater.setId(groundWater.getId());
        }
        tbGroundWater.setSource_id(sourceId);
        tbGroundWater.setAquifer_name(groundWater.getAquifer_name());
        tbGroundWater.setAquifer_source(score.floatValue());
        tbGroundWater.setContainment(groundWater.getContainment());
        tbGroundWater.setCreate_time(new Date());
        tbGroundWater.setDepth_to_auuifer(groundWater.getDepth_to_auuifer());
        tbGroundWater.setHaza_quantity(groundWater.getHasWasteQuantity());
        tbGroundWater.setIs_delete(0);
        tbGroundWater.setIs_karst(groundWater.getIs_karst());
        tbGroundWater.setIsCalculate(groundWater.getIsCalculate());
        tbGroundWater.setMobility_type(1);
        tbGroundWater.setNear_well(groundWater.getNear_well());
        tbGroundWater.setNet_precip(groundWater.getNet_precip());
        tbGroundWater.setObserved_release(groundWater.getObserved_release());
        tbGroundWater.setPop_concent1(groundWater.getPop_concent1());
        tbGroundWater.setPop_concent2(groundWater.getPop_concent2());
        tbGroundWater.setPopulation(groundWater.getPopulation().floatValue());
        tbGroundWater.setPot_concent(groundWater.getPot_concent());
        tbGroundWater.setPotential_to_release(groundWater.getPotential_to_release().floatValue());
        tbGroundWater.setResource(groundWater.getResource());
        tbGroundWater.setTarget(groundWater.getTarget().floatValue());
        tbGroundWater.setTravel_time(groundWater.getTravel_time());
        tbGroundWater.setUpdate_time(new Date());
        tbGroundWater.setWaste_characteristics(groundWater.getWaste_characteristics());
        tbGroundWater.setWell_pro_area(groundWater.getWell_pro_area());
        tbGroundWater.setUncapped_score(uncappedScore.floatValue());
        if(tbGroundWater.getIsCalculate()==1){
            tbGroundWaterMapper.updateIsCaculate(sourceId);
        }
        return tbGroundWater;
    }

    @Override
    public List<TbScore> getGroundWaterList(int sourceId) {
        List<TbScore> tbScoreList = new ArrayList<>();
        TbGroundWater tbGroundWater = new TbGroundWater();
        tbGroundWater.setSource_id(sourceId);
        tbGroundWater.setIs_delete(0);
        if (tbGroundWaterMapper.selectCount(tbGroundWater)==0){
            return tbScoreList;
        }
         tbScoreList = tbGroundWaterMapper.getGroundWaterIsCaculate(sourceId);
        tbScoreList.addAll(tbGroundWaterMapper.getGroundWaterList(sourceId));
        tbScoreList.get(0).setUsed(true);
        return tbScoreList;
    }

    @Override
    public TbGroundWater getGroundWaterById(int id,int sourceId) {
        TbGroundWater tbGroundWater = new TbGroundWater();
        tbGroundWater.setId(id);
        tbGroundWater.setSource_id(sourceId);
        return tbGroundWaterMapper.selectOne(tbGroundWater);
    }

    @Override
    public boolean updateGroundWater(GroundWater groundWater,int sourceId) {
        TbGroundWater tbGroundWater = caculateGroundWater(groundWater,sourceId);
        if (tbGroundWaterMapper.updateById(tbGroundWater)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean insertGroundWater(GroundWater groundWater,int sourceId) {
        TbGroundWater tbGroundWater = caculateGroundWater(groundWater,sourceId);
        if (tbGroundWaterMapper.insert(tbGroundWater)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean delGroundWater(int id, int sourceId) {
        return tbGroundWaterMapper.delGroundWater(id,sourceId);
    }
}
